package com.huawei.zyb.taskManager.infrastructure.persistence.mapper;

import com.huawei.zyb.taskManager.domain.requirement.entity.ReferenceDocument;
import com.huawei.zyb.taskManager.domain.requirement.valueobject.DocumentType;
import com.huawei.zyb.taskManager.domain.requirement.valueobject.ReferenceDocumentId;
import com.huawei.zyb.taskManager.domain.requirement.valueobject.RequirementId;
import com.huawei.zyb.taskManager.infrastructure.persistence.po.ReferenceDocumentPO;

/**
 * 参考文档领域对象与持久化对象映射器
 */
public class ReferenceDocumentDomainMapper {
    
    /**
     * 领域对象转持久化对象
     */
    public static ReferenceDocumentPO toReferenceDocumentPO(ReferenceDocument document) {
        if (document == null) {
            return null;
        }
        
        ReferenceDocumentPO po = new ReferenceDocumentPO();
        po.setDocumentId(document.getId().getValue());
        po.setRequirementId(document.getRequirementId().getValue());
        po.setTitle(document.getTitle());
        po.setUrl(document.getUrl());
        po.setDescription(document.getDescription());
        po.setType(document.getType().getCode());
        po.setUploader(document.getUploader());
        po.setViewCount(document.getViewCount());
        po.setCreatedAt(document.getCreatedAt());
        po.setUpdatedAt(document.getUpdatedAt());
        po.setLastViewedAt(document.getLastViewedAt());
        
        return po;
    }
    
    /**
     * 持久化对象转领域对象
     */
    public static ReferenceDocument toReferenceDocument(ReferenceDocumentPO po) {
        if (po == null) {
            return null;
        }
        
        return ReferenceDocument.restore(
            ReferenceDocumentId.of(po.getDocumentId()),
            RequirementId.of(po.getRequirementId()),
            po.getTitle(),
            po.getUrl(),
            po.getDescription(),
            DocumentType.fromCode(po.getType()),
            po.getUploader(),
            po.getViewCount() != null ? po.getViewCount() : 0,
            po.getCreatedAt(),
            po.getUpdatedAt(),
            po.getLastViewedAt()
        );
    }
}